package kuba.service;


import java.util.Collection;

import kuba.bean.Author;
import kuba.bean.CD;
import kuba.common.BeanValidator;
import kuba.daos.AuthorDAO;
import kuba.daos.CdDAO;
import kuba.event.BasicEvent;
import kuba.event.EventPublisher;

import org.springframework.beans.factory.annotation.Autowired;

public class Service {

    @Autowired
    AuthorDAO authorDAO;
    @Autowired
    CdDAO cdDAO;
    @Autowired
    BeanValidator validator;
    @Autowired
    EventPublisher publisher;

    public Collection<Author> getAllAuthors() {
        return  authorDAO.getAll();
    }

    public Collection<CD> getAuthorCds (Author author){
        return cdDAO.getByAuthor(author);
    }

    public void insertAuthor(Author author) {
        if (validator.validateAuthor(author)) {
            authorDAO.insert(author);
            publisher.publish(new BasicEvent(this,"Author added"));
        }
    }

    public void deleteAuthor(Author author) {

        authorDAO.delete(author);
        publisher.publish(new BasicEvent(this,"Author deleted"));
    }
    public void updateAuthor(Author author) {
        if (validator.validateAuthor(author)) {
            authorDAO.update(author);    
            publisher.publish(new BasicEvent(this,"Author upgraded"));
        }
    }

    public Collection<CD> getAllCds() {
        return cdDAO.getAll();
    }

    public void insertCd(CD cd,Author author) {
        if (validator.validateCd(cd)) {
            cdDAO.insert(cd, author);
            publisher.publish(new BasicEvent(this,"CD added"));
        }
    }

    public void updateCd (CD cd, Author author) {
        if (validator.validateCd(cd)) {
            cdDAO.update(cd, author);
            publisher.publish(new BasicEvent(this,"CD updated"));
        }
    }

    public void deleteCd (CD cd) {
        cdDAO.delete(cd);
        publisher.publish(new BasicEvent(this,"CD deleted"));
    }

    public AuthorDAO getAuthorDAO() {
        return authorDAO;
    }

    public void setAuthorDAO(AuthorDAO authorDAO) {
        this.authorDAO = authorDAO;
    }

    public CdDAO getCdDAO() {
        return cdDAO;
    }

    public void setCdDAO(CdDAO cdDAO) {
        this.cdDAO = cdDAO;
    }

    public BeanValidator getValidator() {
        return validator;
    }

    public void setValidator(BeanValidator validator) {
        this.validator = validator;
    }

    public EventPublisher getPublisher() {
        return publisher;
    }

    public void setPublisher(EventPublisher publisher) {
        this.publisher = publisher;
    }
    
    
}
